package com.kevin.springboot.controller;

import com.kevin.springboot.bo.ResultBO;
import com.kevin.springboot.domain.User;
import com.kevin.springboot.utils.CookieUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.UUID;

@Slf4j
@RequestMapping("/user")
@RestController
public class Knife4Controller {

    @ApiOperation(value = "用户登录测试接口", notes = "用户登录试接口000")
    @PostMapping("login")
    public ResultBO<User> login(@RequestBody User user, HttpServletRequest request, HttpServletResponse response){

        log.info("传入的user对象为：{}", user);
        ResultBO results = new ResultBO();

        String cookieName = UUID.randomUUID().toString();
        Cookie cookie = new Cookie("ss_at", cookieName);
        log.info("生成的 cookie 对象为：{}, cookies 为:{}", cookie, cookieName);
        response.addCookie(cookie);

        results.setCode(200);
        results.setContent(cookieName);
        results.setMsg("调用测试接口成功！");
        results.setSucceed(true);

        log.info("调用测试接口成功");
        return results;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用户名称", required = true, dataType = "String", paramType = "path", example = "默认值")
    })
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "接口返回成功状态！"),
            @ApiResponse(code = 500, message = "接口返回未知错误！")
    })
    @ApiOperation(value = "查询测试接口", notes = "查询测试接口1111")
    @GetMapping("getUser/{name}/{message}")
    public ResultBO<User> getUser(@PathVariable String name, @PathVariable String message, HttpServletRequest request){

        log.info("传入的name为：{}, 传入的message为：{}", name, message);
        log.info("传入的request为：{}", request);
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest httpServletRequest = attributes.getRequest();
        String accessToken = CookieUtils.getCookieValue(httpServletRequest, "ss_at");
        log.info("获取到的accessToken为：{}", accessToken);
        ResultBO results = new ResultBO();
        User user = new User();
        user.setName(name);
        user.setMessage(message);

        results.setCode(200);
        results.setContent(user);
        results.setMsg("调用测试接口成功！");
        results.setSucceed(true);

        log.info("调用测试接口成功");

        return results;
    }

    @ApiOperation(value = "保存测试接口", notes = "保存测试接口222")
    @PostMapping("saveUser")
    public ResultBO<User> saveUser(@RequestBody User user){

        log.info("传入的user对象为：{}", user);
        ResultBO results = new ResultBO();

        results.setCode(200);
        results.setContent(user);
        results.setMsg("调用测试接口成功！");
        results.setSucceed(true);

        log.info("调用测试接口成功");

        return results;
    }

    @ApiOperation(value = "上传测试接口", notes = "上传测试接口333")
    @PostMapping("upload")
    public ResultBO<User> upload(@RequestParam("file") List<MultipartFile> uploadFiles){

        log.info("传入的uploadFiles对象集合为：{}", uploadFiles);
        ResultBO results = new ResultBO();

        results.setCode(200);
        results.setMsg("调用测试接口成功！");
        results.setSucceed(true);

        log.info("调用测试接口成功");

        return results;
    }
}
